package org.newlucene.core.index;

import java.io.IOException;

import org.newlucene.core.util.BitVector;

final class SegmentMergeInfo
{
	Term term;
	int base;
	SegmentTermInfos tis;
	SegmentReader reader;
	SegmentTermPostings postings;
	int[] docMap = null; // maps around deleted docs

	public SegmentMergeInfo(int b, SegmentTermInfos te, SegmentReader r) throws IOException
	{
		base = b;
		reader = r;
		tis = te;
		term = te.term();
		postings = new SegmentTermPostings(r);

		if (reader.deletedDocs != null)
		{
			// build array which maps document numbers around deletions
			BitVector deletedDocs = reader.deletedDocs;
			int maxDoc = reader.maxDoc();
			docMap = new int[maxDoc];
			int j = 0;
			for (int i = 0; i < maxDoc; i++)
			{
				if (deletedDocs.get(i))
				{
					docMap[i] = -1;
				}
				else
				{
					docMap[i] = j++;
				}
			}
		}
	}

	public boolean next() throws IOException
	{
		if (tis.next())
		{
			term = tis.term();
			return true;
		}
		else
		{
			term = null;
			return false;
		}
	}

	public void close() throws IOException
	{
		tis.close();
		postings.close();
	}
}